<template>
  <div class="login">
    <div class="login_box">
      <!-- 头部区域 -->
      <div class="avatar_box">
        <img src="../assets/weixin.jpg" alt="" />
        <!-- <img src="../assets/logo.png" alt="" /> -->
      </div>
      <!-- 登录表单区域 -->
      <el-form
        :model="LoginForm"
        :rules="LoginFormRules"
        label-width="0px"
        class="form_box"
        ref="LoginFormRef"
      >
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="LoginForm.username"
            placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="LoginForm.password"
            prefix-icon="el-icon-s-goods"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="buttons">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data() {
    return {
      // 这是登录表单的数据绑定对象
      LoginForm: {
        username: '',
        password: '',
      },
      // 这是表单的验证规则对象
      LoginFormRules: {
        // 验证用户是否合法
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '长度在 3 到 10 个字符',
            trigger: 'blur',
          },
        ],
        // 验证密码是否合法
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur',
          },
        ],
      },
    };
  },
  methods: {
    // 点击重置按钮,重置表单信息
    resetLoginForm() {
      this.$refs.LoginFormRef.resetFields();
    },
    login() {
      this.$refs.LoginFormRef.validate(async (valid) => {
        // console.log(valid);
        if (!valid) return;
        const { data: res } = await this.$axios.post('login', this.LoginForm);
        // console.log(res);
        if (res.meta.status !== 200) return this.$message.error('登录失败！');
        this.$message.success('登录成功！');
        //1.将登陆成功之后的token保存到客户端的sessionStorage中
        //   1.1项目中出了登录之外的其他API接口，必须在登录之后才能访问
        //   1.2token 只应该在当前网站打开期间生效,所以将 token 保存在 sessionStorage 中
        window.sessionStorage.setItem('token', res.data.token);
        // 2.通过编程式导航跳转到后台主页
        this.$router.push('/home');
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  height: 100%;
  position: relative;
  background-image: -webkit-linear-gradient(left, #be93c5, #7bc6cc);
  .login_box {
    width: 450px;
    height: 300px;
    border-radius: 5px;
    background-color: #eee;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .avatar_box {
      width: 130px;
      height: 130px;
      padding: 10px;
      border: 10px solid #eee;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      border-radius: 50%;
      img {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 150px;
        height: 150px;
        border-radius: 50%;
        background-image: -webkit-linear-gradient(left, #be93c5, #7bc6cc);
      }
    }
    .form_box {
      width: 100%;
      height: 200px;
      padding: 20px;
      box-sizing: border-box;
      position: absolute;
      top: 100px;
      left: 0;
      right: 0;
      .buttons {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>
